centres = function() {
	//定义添加修改窗体
	var CENTREADDOREDITWIN = null;
	//定义CKEDITR组件
	var CENTRECKEDITTXTCOM = null;
	//定义搜索窗体
	var CENTRESEARCHWIN = null;
	//定义栏目STORE
	var CENTRECHANNELSTORE = new Store({
		fields: ['id', 'str'],
		proxy: {
			type: 'ajax',
	        url: '../admin/centre.do?indextochannel'
		},
		autoLoad: false
	});
	
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		//定义GRID窗体
		panel : function() {
			//初始化数据
			CENTRECHANNELSTORE.load();
			
			var store = new Store({
				fields: ['id', 'title', 'updateDate', 'txt', 'channel'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/centre.do?index',
			        reader: {
			            type: 'json',
			            root: 'datas',
			            totalProperty: 'total'
			        }
			    }
			});
			
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				title: '',
				channel: -1
			};
			store.load();
			
			var pageToolBar = new PagingToolbar({
	            store: store,
	            draggable : false,
	            displayInfo: true
	        });
			
			var grid = new Grid({
				title: '中心简介列表',
				id: 'centregrid',
				region: 'center',
				store: store,
				minColumnWidth: 35,
				stripeRows: true,
				bbar : pageToolBar,
				tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						store.load();
					}
				}, "-", {
					text: '搜索',
					scale: 'medium',
					iconCls: 'icon-search',
					handler: function() {
						centres.searchAction();
					}
				}, "-", {
					id: 'centreEdit',
					text: '修改',
					scale: 'medium',
					iconCls: 'icon-edit',
					disabled: true,
					handler: function() {
						centres.editAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '标题', dataIndex: 'title',width: 200},
		             {header: '栏目', dataIndex: 'channel', width: 200, renderer: function(v) {
		            	 return CENTRECHANNELSTORE.getById(v).get('str'); 
		             }},
		             {header: '更新时间', dataIndex: 'updateDate', width: 200}
				]
			});
			
			var panel = new Panel({
				border: false,
				layout: 'border',
				items: [grid]
			});
			return panel;
		},
		
		//定义搜索窗体
		searchWin: function() {
			if (CENTRESEARCHWIN == null) {
				var channelStore = new Store({
					fields: ['id', 'str'],
					data: [
					   {'id': -1, 'str': '全部'}
					]
				});
				CENTRECHANNELSTORE.each(function(v) {
					channelStore.add(v);
				});
				
				CENTRESEARCHWIN = new Window({
					id: 'centreSearchWin',
				    title: '搜索中心简介',
				    height: 170,
				    width: 450,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						autoScroll: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	fieldLabel: '标题',
				        	name: 'title'
				        }, {
				        	fieldLabel: '栏目',
				        	xtype: 'combo',
			             	name: 'channel',
			             	store: channelStore,
			                displayField: 'str',
			                valueField: 'id',
			                queryMode: 'local',
			                editable: false,
			                value: -1
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		centres.searchSureAction(fom);
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		CENTRESEARCHWIN.hide();
				        	}
				        }]
				    }],
				    listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        	}
			        }
				});
			}
			CENTRESEARCHWIN.show();
		},
		
		//定义添加修改窗体
		addOrEditWin: function() {
			if (CENTREADDOREDITWIN == null) {
				CENTREADDOREDITWIN = new Window({
					id: 'centreAddOrEditWin',
				    title: '中心简介信息编辑',
				    height: 550,
				    width: 1000,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	frame: false,
						autoHeight: true,
						autoScroll: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'id',
				        	hidden: true
				        }, {
				        	fieldLabel: '标题',
				        	name: 'title',
				            allowBlank: false
				        }, {
				        	xtype: 'textareafield',
				    		name: 'txt',
				    		hidden: true
				        }, {
				    		xtype: 'fieldcontainer',
				            fieldLabel: '内容',
				            layout: 'fit',
				            height: 330,
				            items: [{
				    	    	xtype: 'panel',
				    	    	border: false,
				    	    	html: '<textarea id="_centrefomtxt" class="ckeditor" name="_centrefomtxt"></textarea>',
				    	    	height: 330
				    		}]
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			fom.findField('txt').setValue(CENTRECKEDITTXTCOM.getData());
				        			centres.sumbitAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		CENTREADDOREDITWIN.hide();
				        	}
				        }],
				        listeners: {
				        	afterrender: function() {
				        		CENTRECKEDITTXTCOM = CKEDITOR.replace('_centrefomtxt');
				        	},
				        	beforedestroy: function() {
				        		CENTRECKEDITTXTCOM.destory();
				        	}
				        }
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		this.down('form').getForm().reset();
			        		CENTRECKEDITTXTCOM.setData('');
			        	}
			        }
				});
			}
			CENTREADDOREDITWIN.show();
		},
		
		/*--------------------------------------事件定义--------------------------------------------*/
		//搜索窗体事件
		searchAction: function() {
			centres.searchWin();
		},
		
		//搜索确定事件
		searchSureAction: function(fom) {
			var store = Ext.getCmp('centregrid').getStore();
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				title: fom.findField('title').value,
				channel: fom.findField('channel').value
			};
			store.load();
			CENTRESEARCHWIN.hide();
		},
		
		//添加窗体事件
		addAction: function() {
			centres.addOrEditWin();
		},
		
		//修改窗体事件
		editAction: function() {
			var sel = Ext.getCmp('centregrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要修改的信息！');
			} else {
				centres.addOrEditWin();
				var fom = Ext.getCmp('centreAddOrEditWin').down('form').getForm();
				fom.loadRecord(sel[0]);
				CENTRECKEDITTXTCOM.setData(fom.findField('txt').value);
			}
		},
		
		//提交确认事件
		sumbitAction: function(fom) {
			var url = '../admin/centre.do?edit';
			var id = fom.findField('id').value;
			if (id == '') {
				url = '../admin/centre.do?add';
			}
			FormSubmit(fom, url, {}, function(obj) {
				Ext.getCmp('centregrid').getStore().load();
				CENTREADDOREDITWIN.hide();
			});
		},
		
		//删除事件
		removeAction: function() {
			var sel = Ext.getCmp('centregrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的信息！');
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该信息吗？', function(btn) {
					if (btn == "yes") {
						var res = RequestByUrl('../admin/centre.do?remove', {
							id: sel[0].get('id')
						});
						Ext.getCmp('centregrid').getStore().load();
						Ext.Show.msg(res.title, '');
					}
				});
			}
		}
	};
}();